package com.yj.transport.controller;

import com.yj.common.responses.TerminalResponse;
import com.yj.oa.entity.system.OAContract;
import com.yj.oa.entity.system.OAUserInfo;
import com.yj.oa.mapper.OAContractMapper;
import com.yj.oa.service.OAInfoService;
import com.yj.transport.dto.GoodsAcceptanceDto;
import com.yj.transport.dto.OwnershipDto;
import com.yj.transport.entity.TransportInfo;
import com.yj.transport.entity.TransportJournal;
import com.yj.transport.service.TransportInfoService;
import com.yj.transport.service.TransportJournalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by zhengguanyong on 2017/12/6.
 */

@Api(tags = "易煤可视化-运输")
@Controller
@RequestMapping(value = "/transport")
public class TransportController {

    @Autowired
    private TransportInfoService transportInfoService;

    @Autowired
    private TransportJournalService transportJournalService;

    @Autowired
    OAContractMapper oaContractMapper;

    @Autowired
    OAInfoService oaInfoService;

    @ApiOperation("运输信息创建")
    @PostMapping("/create-transport-info")
    @ResponseBody
    public TerminalResponse<Objects> createTransportInfo(@RequestHeader("token") String token,@RequestBody TransportInfo transportInfo) {
        OAContract oaContract = oaContractMapper.getContractByMainID(transportInfo.getMain_id());
        if (oaContract != null) {
            OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);
            transportInfo.setUser_id(oaUserInfo.getUser_id());
            transportInfoService.createTransportInfo(transportInfo);
            return new TerminalResponse<>(true, "运输信息创建成功", null);
        }
        return new TerminalResponse<>(false, "运输信息创建失败,该业务id不存在", null);
    }

    @ApiOperation("运输信息详情查看")
    @GetMapping("/get-transport-info")
    @ResponseBody
    public TerminalResponse<TransportInfo> getTransportInfoDetailById(@ApiParam(value = "运输信息ID") @RequestParam("id") Integer id) {
        TransportInfo transportInfo = transportInfoService.getTransportInfoDetailById(id);
        if (transportInfo != null) {
            return new TerminalResponse<>(true, "成功", transportInfo);
        } else {
            return new TerminalResponse<>(false, "该运输信息id不存在", null);
        }
    }

    @ApiOperation("运输信息列表")
    @GetMapping("/get-transport-info-list")
    @ResponseBody
    public TerminalResponse<List<TransportInfo>> getTransportInfoListByMainId(@ApiParam(value = "业务ID") @RequestParam("main_id") String main_id) {
        List<TransportInfo> transportInfoList = transportInfoService.getTransportInfoListByMainId(main_id);
        return new TerminalResponse<>(true, "成功", transportInfoList);
    }


    @ApiOperation("运输信息更新(即运输轨迹创建)")
    @PostMapping("/create-transport-journal")
    @ResponseBody
    public TerminalResponse<Objects> createTransportJournal(@RequestBody TransportJournal transportJournal) {
        TransportInfo transportInfo = transportInfoService.getTransportInfoDetailById(transportJournal.getTransport_info_id());
        if (transportInfo == null) {
            return new TerminalResponse<>(false, "运输轨迹创建失败,该运输信息id不存在", null);
        }
        transportJournalService.createTransportJournal(transportJournal);
        return new TerminalResponse<>(true, "运输轨迹创建成功", null);
    }

    @ApiOperation("运输轨迹列表")
    @GetMapping("/get-transport-journal-list")
    @ResponseBody
    public TerminalResponse<List<TransportJournal>> getTransportJournalListByMainId(@ApiParam(value = "运输信息ID") @RequestParam("transport_info_id") Integer transport_info_id) {
        List<TransportJournal> transportJournalList = transportJournalService.getTransportJournalListByInfoId(transport_info_id);
        return new TerminalResponse<>(true, "成功", transportJournalList);
    }

    @ApiOperation("货权更新")
    @PostMapping("/update-ownership")
    @ResponseBody
    public TerminalResponse<Objects> updateOwnership(@RequestBody OwnershipDto ownershipDto) {
        if (transportInfoService.getTransportInfoDetailById(ownershipDto.getTransport_info_id()) == null) {
            return new TerminalResponse<>(false, "运输信息ID不存在", null);
        }
        int success = transportInfoService.updateOwnership(ownershipDto.getTransport_info_id(), ownershipDto.getOwnership());
        if (success > 0) {
            return new TerminalResponse<>(true, "货权更新成功", null);
        } else {
            return new TerminalResponse<>(false, "货权更新失败", null);
        }
    }

    @ApiOperation("货物验收")
    @PostMapping("/goods-acceptance")
    @ResponseBody
    public TerminalResponse<Objects> goodsAcceptance(@RequestBody GoodsAcceptanceDto goodsAcceptanceDto) {
        if (transportInfoService.getTransportInfoDetailById(goodsAcceptanceDto.getTransport_info_id()) == null) {
            return new TerminalResponse<>(false, "运输信息ID不存在", null);
        }
        int success = transportInfoService.goodsAcceptance(goodsAcceptanceDto.getTransport_info_id(), goodsAcceptanceDto.getAcceptance_status(), goodsAcceptanceDto.getOwnership());
        if (success > 0) {
            return new TerminalResponse<>(true, "货物验收成功", null);
        } else {
            return new TerminalResponse<>(false, "货物验收失败", null);
        }
    }

    @ApiOperation("运输动态-查询所有货物当前位置-未登录用户")
    @GetMapping("/get-dynamic-info-list-guest")
    @ResponseBody
    public TerminalResponse<List<TransportInfo>> getDynamicInfoListByNone() {
        List<TransportInfo> transportInfoList = transportInfoService.getDynamicInfoListByNone();
        return new TerminalResponse<>(true, "成功", transportInfoList);
    }

    @ApiOperation("运输动态-查询所有货物当前位置-用户登录后-业务员和领导")
    @GetMapping("/get-dynamic-info-list-token")
    @ResponseBody
    public TerminalResponse<List<TransportInfo>> getDynamicInfoListByUserId(@RequestHeader("token") String token) {
        List<TransportInfo> transportInfoList = transportInfoService.getDynamicInfoListByUserId(token);
        return new TerminalResponse<>(true, "成功", transportInfoList);
    }

    @ApiOperation("运输动态-车号模糊查询sug")
    @GetMapping("/search-dynamic-info")
    @ResponseBody
    public TerminalResponse<List<String>> searchDynamicInfoByPlateNumber(@ApiParam(value = "运输车牌号(火车车号，船名，汽车牌照)") @RequestParam("plate_number") String plate_number) {
        List<String> transportInfoList = transportInfoService.searchDynamicInfoByPlateNumber(plate_number);
        return new TerminalResponse<>(true, "成功", transportInfoList);
    }

    @ApiOperation("运输动态-获取单个车号位置")
    @GetMapping("/get-dynamic-info")
    @ResponseBody
    public TerminalResponse<TransportInfo> getDynamicInfoByPlateNumber(@ApiParam(value = "运输车牌号(火车车号，船名，汽车牌照)") @RequestParam("plate_number") String plate_number) {
        TransportInfo transportInfo = transportInfoService.getDynamicInfoByPlateNumber(plate_number);
        return new TerminalResponse<>(true, "成功", transportInfo);
    }

    @ApiOperation("运输动态-获取单个车号轨迹")
    @GetMapping("/get-dynamic-journal")
    @ResponseBody
    public TerminalResponse<List<TransportJournal>> getDynamicJournalByPlateNumber(@ApiParam(value = "运输车牌号(火车车号，船名，汽车牌照)") @RequestParam("plate_number") String plate_number) {
        List<TransportJournal> transportJournalList = transportInfoService.getDynamicJournalByPlateNumber(plate_number);
        return new TerminalResponse<>(true, "成功", transportJournalList);
    }

    @ApiOperation("运输动态-运输中车辆统计-未登录")
    @GetMapping("/count-dynamic-info-guest")
    @ResponseBody
    public TerminalResponse<Map<String, Integer>> countDynamicInfoGuest() {
        Map<String, Integer> countDynamicInfo = transportInfoService.countDynamicInfoGuest();
        return new TerminalResponse<>(true, "成功", countDynamicInfo);
    }

    @ApiOperation("运输动态-运输中车辆统计-登录后")
    @GetMapping("/count-dynamic-info-token")
    @ResponseBody
    public TerminalResponse<Map<String, Integer>> countDynamicInfoToken(@RequestHeader("token") String token) {
        Map<String, Integer> countDynamicInfo = transportInfoService.countDynamicInfo(token);
        return new TerminalResponse<>(true, "成功", countDynamicInfo);
    }
}